/**system core utils for safty utils,such as user login/ alter password/ update account info
 * fuzhou huilin corp.ltd @author lms 2016.07.14
 */
package com.fzrskj.cloud.free.sys.service;

import com.fzrskj.cloud.free.consts.AppCode;
import com.fzrskj.cloud.free.consts.CConsts;
import com.fzrskj.cloud.free.core.tool.CEncryptTool;
import com.fzrskj.cloud.free.core.tool.CJsonTool;
import com.fzrskj.cloud.free.core.tool.CSessionTool;
import com.fzrskj.cloud.free.core.utils.CloudContextUtils;
import com.fzrskj.cloud.free.core.utils.EncryptUtils;
import com.fzrskj.cloud.free.sys.bean.*;
import com.fzrskj.cloud.free.sys.utils.CUserUtils;
import ms.core.consts.SysErr;
import ms.core.object.CObject;
import ms.core.support.Session;
import ms.core.tool.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CSaftyService extends CObject{

	private List<String> forbidFunc = new ArrayList<String>() {{
		//其他服务管理
		//add("65");
		//其他服务配置
		//add("3f");
		//微服务管理
		add("3h");
		//主机操作记录
		add("63");
		//数据字典
		add("78");
	}};

	private String formatSysUser(User u){
		String data = JsonTool.beanToJson(u);
		data = JsonTool.delNode(data, "password");		//remove password node
		data = JsonTool.delNode(data, "checkcode");	//remove checkcode node
		data = JsonTool.delNode(data, "funcs");		//remove functions node
		data = JsonTool.addNode(data, "utypeName", CConsts.indexOfUserType(u.getUtype()));
		data = JsonTool.addNode(data, "stateName", CConsts.indexOfUserState(u.getState()));
		data = JsonTool.addNode(data, "createTime", DateTimeTool.dateToDateTimeString(u.getCreateTime()));	//format datetime
		data = JsonTool.addNode(data, "visitTime", DateTimeTool.dateToDateTimeString(u.getVisitTime()));	//format datetime
		return data;
	}
	
	//get check code
	public String getCode(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		try{
			resp.setContentType("image/jpeg");
			String verifyCode = AuthCodeTool.generateCheckCode(4);
			CSessionTool.setCheckCodeCloud(sid, verifyCode);					//set login check code
			ServletOutputStream ros = resp.getOutputStream();
			AuthCodeTool.outputImage(200,80,ros,verifyCode);
			ros.flush();
			ros.close();
			return null;
		}catch(IOException e){
			return e.getMessage();
		}
	}

	//if current user has logined
	private static boolean logined(HttpServletRequest request, String sid){
		Session s = CSessionTool.getSessionCloud(sid);
		if ((s==null) || StrTool.isBlankStr(s.getUname())) {
			return false;
		}

		int uid		   = CookieTool.getUid(request);
		String account = CookieTool.getAccount(request);
		String said    = CookieTool.getSAID(request);
		if ((uid<=0) || (uid!=s.getUID()) || StrTool.isBlankStr(account) || StrTool.isBlankStr(said))
			return false;

		return said.equals(CEncryptTool.encodeSAID(sid, uid, account));
	}

	/**user login api
	 * @param req HttpServletRequest object
	 * @param resp HttpServletResponse object
	 * @param p login parameter,such like {"account":"admin@126.com","password":"admin","checkcode":"xxx"}
	 * @param sid user's password
	 * by fuzhou huilin corp.ltd @author lms 2016.07.18
	 */
	public String userLogin(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		p = CloudContextUtils.getP();
		String ret;
		User tmp = (User) JsonTool.jsonToBean(p, User.class);
		if (tmp==null){
			this.log(p+"-->invalid login parameter");			
			return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
		}

		Session session = CSessionTool.getSessionCloud(sid);
		if (StrTool.isBlankStr(tmp.getCheckcode()) || !session.getCheckCode().toLowerCase().equals(tmp.getCheckcode().toLowerCase())) {
			return XmlCodeParser.formatResultTips(false, SysErr.SYS_CHECKCODE_ERR);
		}

		tmp.setPassword(EncryptUtils.decodePublic(tmp.getPassword()));

		String visitIp = HttpTool.getRequestRemoteIp(req);
		CUserUtils obj = new CUserUtils();
		User u = obj.userLogin(tmp.getAccount(), tmp.getPassword(), visitIp);
		String code  = obj.getErrCode(); 
		obj.dispose();
		if (u==null){
			return XmlCodeParser.formatResultTips(false, code);
		}
		if(u.getState()==1){
			return XmlCodeParser.formatResultTips(false, AppCode.T_status01);
		}else if (u.getState()==2) {
			return XmlCodeParser.formatResultTips(false,AppCode.T_status02);
		}
		String data = formatSysUser(u);		
		ret = XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, data);
		//set user session
		Session ssn = new Session();
		ssn.setId(u.getId());
		ssn.setUID(u.getAccountId());
		ssn.setUname(u.getAccount());
		ssn.setUtype(0);
		ssn.setNick(u.getNick());
		ssn.setFuncs(u.getFuncs());
		ssn.setData(u.getRoles());
		CSessionTool.setLoginSessionCloud(sid, ssn);
		//set user cookie
		Cookies cks = new Cookies();
		cks.setId(u.getId());
		cks.setUid(u.getAccountId());
		cks.setAccount(u.getAccount());
		cks.setUname(u.getAccount());
		cks.setNick(u.getNick());
		cks.setSaid(CEncryptTool.encodeSAID(sid, u.getAccountId(), u.getAccount()));

		EncryptUtils.makeSession(resp, req);

		CookieTool.setLoginCookie(resp, sid, cks);
		return ret;
	}

	/**user logout
	 * @param req HttpServletRequest of current user
	 * @param resp HttpServletResponse of current user
	 * @param p function parameter
	 * @param sid session id of current user
	 * @author by fuzhou huilin corp.ltd lms 2016.07.18
	 */
	public String userLogout(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		if (!logined(req, sid)){
			return XmlCodeParser.formatResultTips(false, SysErr.USER_NOT_LOGIN);
		}

		CSessionTool.delSessionCloud(sid);
		CookieTool.clearLoginCookie(resp);
		return XmlCodeParser.formatResultTips(true, SysErr.CODE_SUCCESS);
	}
	
	/**
	 * account has logined?
	 * @param req
	 * @param resp
	 * @param p
	 * @param sid
	 * @return
	 */
	public String logined(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		if (!logined(req, sid)){
			return XmlCodeParser.formatResultTips(false, SysErr.USER_NOT_LOGIN);
		}
		String data = String.format("{\"uname\":\"%s\"}", CSessionTool.getAccountCloud(sid));
		return XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, data);
	}

	/**
	 * to get admin's function menu
	 * @param req
	 * @param resp
	 * @param p
	 * @param sid
	 * @return
	 */
	public String getUserMenu(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		String userFuns = CSessionTool.getSessionCloud(sid).getFuncs();
		CUserUtils utls = new CUserUtils();

		List<Func> rets = utls.queryUserFunc(userFuns);


		if (null!=rets && CloudContextUtils.getAgentId() > 0) {
			//去掉代平台不支持的功能
			List<Func> newList = new ArrayList<>();
			for (Func fun: rets) {
				if (!this.forbidFunc.contains(fun.getFunId())) {
					newList.add(fun);
				}
			}
			rets = new ArrayList<>();
			rets.addAll(newList);
		}

		String err = utls.getErrCode();
		utls.dispose();
		if (!err.equals(SysErr.CODE_SUCCESS)) {
			return XmlCodeParser.formatResultTips(false, err);
		}

		if (null == rets) {
			return XmlCodeParser.formatResultTips(false, SysErr.USER_NOT_LOGIN);
		}
		
		return XmlCodeParser.formatResultData(true, err, JsonTool.listBeanToJson(rets));
	}
	
	/**to add admin account
	 * @param req
	 * @param resp
	 * @param p,it's format is like this:p={"account":"xx","password":"xxxx","department":"department","duty":"duty",
	 * 										"roles":"roleName","nick":"xxx","mobile":"xxxxx","officeTel":"xxxxx","utype":n}
	 * @param sid
	 * @return failure return {"code":"failure","tips":"error info"}, success with {"code":"success","tips":"info","data":{"uid":n}}
	 */
	public String addUser(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		User user = (User) JsonTool.jsonToBean(p, User.class);
		if ((user==null) || StrTool.isBlankStr(user.getAccount())){
			this.log(p+"-->invalid new user parameter");
			return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
		}

		CUserUtils obj = new CUserUtils();
		int rid = obj.addUser(user);
		String code = obj.getErrCode();
		obj.dispose();
		if (rid<=0){
			return XmlCodeParser.formatResultTips(false, code);
		}
		
		String data = String.format("{\"rid\":%d,\"uid\":%d}", user.getId(),user.getAccountId());
		return XmlCodeParser.formatResultData(true, code, data);
	}
	
	/**to delete admin account
	 * @param req
	 * @param resp
	 * @param p,it's format is like this:p={"id":n,"account":"xx"}
	 * @param sid
	 * @return failure return {"code":"failure","tips":"error info"}, success with {"code":"success","tips":"info"}
	 */
	public String delUser(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		User user = (User) JsonTool.jsonToBean(p, User.class);
		if ((user==null) || StrTool.isBlankStr(user.getAccount())){
			this.log(p+"-->invalid parameter");
			return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
		}

		CUserUtils obj = new CUserUtils();
		int ret = obj.delUser(user.getId());
		String code  = obj.getErrCode();
		obj.dispose();
		
		boolean result = (ret>=0);
		return XmlCodeParser.formatResultTips(result, code);
	}
	
	/**to modify admin account
	 * @param req
	 * @param resp
	 * @param p,it's format is like this:p={"id":n,"nick":"xxx","department":"department","duty":"duty",
	 * 										"mobile":"xxxxx","officeTel":"xxxxx","utype":n}
	 * @param sid
	 * @return {"code":"failure/success","tips":"runtime info"}
	 */
	public String modUser(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		User user = (User) JsonTool.jsonToBean(p, User.class);
		if ((user==null) || user.getId()<=0 || StrTool.isBlankStr(user.getNick())){
			this.log(p+"-->invalid parameter");
			return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
		}

		CUserUtils obj = new CUserUtils();
		int ret = obj.modUser(user);
		String code  = obj.getErrCode();
		obj.dispose();
		
		boolean result = (ret>0);
		return XmlCodeParser.formatResultTips(result, code);
	}
	
	/**to set admin role
	 * @param req
	 * @param resp
	 * @param p,it's format is like this:p={"id":n,"account":"xxx@kkk.com","roles":"role1,role2,role3"}
	 * @param sid
	 * @return {"code":"failure/success","tips":"runtime info"}
	 */
	public String setUserRole(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		User user = (User) JsonTool.jsonToBean(p, User.class);
		if ((user==null) || StrTool.isBlankStr(user.getAccount())){
			this.log(p+"-->invalid parameter");
			return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
		}

		CUserUtils obj = new CUserUtils();
		int ret = obj.setUserRole(user.getAccount(),user.getRoles());
		String code  = obj.getErrCode();
		obj.dispose();

		boolean result = (ret>0);
		return XmlCodeParser.formatResultTips(result, code);
	}
	
	/**
	 * to modify user's password
	 * @param req
	 * @param resp
	 * @param p parameter, it's format: p={"id":n,"oldpass","oldpassword","newpass":"newpassword"}
	 * @param sid session id of current user
	 * @return json string
	 * @author by fuzhou huilin corp.ltd lms 2016.08.09
	 */
	public String setUserPassword(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		if (!logined(req, sid)){
			return XmlCodeParser.formatResultTips(false, SysErr.USER_NOT_LOGIN);
		}
		//verify parameter
		Session sObj = CSessionTool.getSessionCloud(sid);

		int rid = CJsonTool.getIntValue(p,"id",-1);
		String oldPass = CJsonTool.getStrValue(p,"oldpass","");
		String newPass = CJsonTool.getStrValue(p,"newpass","");
		if ((rid<=0) || (rid!=sObj.getId()) || (StrTool.isBlankStr(oldPass)) || (StrTool.isBlankStr(newPass))){
			this.log(p+"-->invalid parameter");
			return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
		}

		CUserUtils obj = new CUserUtils();
		int ret = obj.setUserPassword(rid, oldPass, newPass);
		String code  = obj.getErrCode();
		obj.dispose();

		boolean result = (ret>0);
		return XmlCodeParser.formatResultTips(result, code);
	}
	
	/**
	 * to modify user's status
	 * @param req
	 * @param resp
	 * @param p parameter, it's format: p={"id":n,"state",n}
	 * @param sid session id of current user
	 * @return json string
	 * @author by fuzhou huilin corp.ltd 816 2016.10.12
	 */
	public String setUserStatus(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		if (!logined(req, sid)){
			return XmlCodeParser.formatResultTips(false, SysErr.USER_NOT_LOGIN);
		}
		int rid = CJsonTool.getIntValue(p,"id",-1);
		int state = CJsonTool.getIntValue(p,"state",0);
		
		if ((rid<=0)){
			this.log(p+"-->invalid parameter");
			return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
		}
		CUserUtils obj = new CUserUtils();
		int ret = obj.setUserStatus(rid, state);
		String code  = obj.getErrCode();
		obj.dispose();
		boolean result = (ret>0);
		return XmlCodeParser.formatResultTips(result, code);
	}
	
	//p={"page":n,"pageSize":x,"account":xyz}
	public String queryUser(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		int page = CJsonTool.getIntValue(p, "page",1);
		if (page<=0) {
			page = 1;
		}
		int	pageSize = CJsonTool.getIntValue(p, "pagesize",CConsts.PAGE_SIZE);
		if (pageSize<=0) {
			pageSize = CConsts.PAGE_SIZE;
		}
		String account = CJsonTool.getStrValue(p, "account","");

		CUserUtils obj = new CUserUtils();
		List<User> users = obj.queryUser(page, pageSize, account);
		String code = obj.getErrCode();
		if (users==null){
			obj.dispose();
			return XmlCodeParser.formatResultTips(false, code);
		}
		
		int count= obj.queryUser(account);
		code = obj.getErrCode();
		obj.dispose();
		int pages= Math.round((count+pageSize-1)/pageSize);
		String data="[";
		for (User u: users){
			data = data+formatSysUser(u)+",";
		}
		data = StrTool.deleteCharAt(data, data.length()-1);
		data = data+"]";
		return XmlCodeParser.formatResultPageData(code, page, pages, pageSize, count, data);
	}

	/**to add role
	 * @param req HttpServletRequest of current user
	 * @param resp HttpServletResponse of current user
	 * @param p function parameter,the format is: p={"roleName":"xxx","roleCName":"xxx","remark":"xxx"}
	 * @param sid session id of current user
	 * @author by fuzhou huilin corp.ltd lms 2016.08.03
	 */
	public String newRole(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		Role role = (Role) CJsonTool.jsonToBean(p, Role.class);
		if ((role==null) || StrTool.isBlankStr(role.getRoleName())){
			this.log(p+"-->invalid parameter");
			return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
		}

		CUserUtils obj = new CUserUtils();
		int ret = obj.newRole(role.getRoleName(), role.getRoleCName(), role.getRemark());
		String code = obj.getErrCode();
		obj.dispose();
		if (ret<=0){
			return XmlCodeParser.formatResultTips(false, code);
		}
		String data = String.format("{\"id\":%d}",ret);
		return XmlCodeParser.formatResultData(true, code, data);
	}

	/**to update role information
	 * @param req HttpServletRequest of current user
	 * @param resp HttpServletResponse of current user
	 * @param p function parameter,it's format: {"roleName":"xxx"}
	 * @param sid session id of current user
	 * @return json string
	 * @author by fuzhou huilin corp.ltd lms 2016.08.08
	 */
	public String delRole(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		String roleName = CJsonTool.getStrValue(p, "roleName", "");
		if (StrTool.isBlankStr(roleName)){
			this.log(p+"-->invalid role name parameter");
			return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
		}

		//remove user's role information
		CUserUtils obj = new CUserUtils();
		int ret = obj.delRole(roleName);
		String code = obj.getErrCode();
		obj.dispose();
		
		boolean result = (ret>=0);
		return XmlCodeParser.formatResultTips(result, code);
	}

	/**to update role information
	 * @param req HttpServletRequest of current user
	 * @param resp HttpServletResponse of current user
	 * @param p function parameter,it's format: {"id":n,"cname":"role's CN name","remark":"remark info"}
	 * @param sid session id of current user
	 * @return json string	 
	 * @author by fuzhou huilin corp.ltd lms 2016.07.28
	 */
	public String modRole(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		int id	= CJsonTool.getIntValue(p, "id", -1);
		String roleCName = CJsonTool.getStrValue(p, "cname",null);
		Integer groupCode = CJsonTool.getIntValue(p, "groupCode", 0);
		String remark = CJsonTool.getStrValue(p, "remark", "");
		if ((id==-1) || StrTool.isBlankStr(roleCName)){
			this.log(p+"-->invalid json, can't parase role update info");
			return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
		}

		CUserUtils obj = new CUserUtils();
		int ret = obj.modRole(id, roleCName, remark);
		String code = obj.getErrCode();
		obj.dispose();
		
		boolean result = (ret>0);
		return XmlCodeParser.formatResultTips(result, code);
	}

	/**to set role function
	 * @param req HttpServletRequest of current user
	 * @param resp HttpServletResponse of current user
	 * @param p function parameter,it's format: {"id":n,"funs":"fun1,fun2,fun3,fun4..."}
	 * @param sid session id of current user
	 * @return json string
	 * @author by fuzhou huilin corp.ltd lms 2016.07.29
	 */
	public String setRoleFun(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		int id	= CJsonTool.getIntValue(p, "id", -1);
		String funs = CJsonTool.getStrValue(p, "funs",null);
		if ((id==-1) || StrTool.isBlankStr(funs)){
			this.log(p+"-->invalid json, can't parase to role object");
			return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
		}

		CUserUtils obj = new CUserUtils();
		int ret = obj.setRoleFun(id, funs);
		String code  = obj.getErrCode();
		obj.dispose();

		boolean result = (ret>0);
		return XmlCodeParser.formatResultTips(result, code);
	}

	/**to query role
	 * @param req HttpServletRequest of current user
	 * @param resp HttpServletResponse of current user
	 * @param p function parameter, no use
	 * @param sid session id of current user
	 * @author by fuzhou huilin corp.ltd lms 2016.07.28
	 */
	public String queryRole(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		int roleId	= CJsonTool.getIntValue(p, "id", -1);
		CUserUtils obj = new CUserUtils();
		List<Role> rs = obj.queryRole(roleId);
		String code = obj.getErrCode();
		obj.dispose();
		if (rs==null){
			return XmlCodeParser.formatResultTips(false, code);
		}
		return XmlCodeParser.formatResultData(true, code, CJsonTool.listBeanToJson(rs));
	}
	
	/**to query system functions
	 * @param req HttpServletRequest of current user
	 * @param resp HttpServletResponse of current user
	 * @param p function parameter, not use
	 * @param sid session id of current user
	 * @author by fuzhou huilin corp.ltd lms 2016.08.03
	 */
	public String queryFuns(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		CUserUtils obj = new CUserUtils();
		List<Func> funs = obj.queryFuns();

		if (null!=funs && CloudContextUtils.getAgentId() > 0) {
			//去掉代平台不支持的功能
			List<Func> newList = new ArrayList<>();
			for (Func fun: funs) {
				if (!this.forbidFunc.contains(fun.getFunId())) {
					newList.add(fun);
				}
			}
			funs = new ArrayList<>();
			funs.addAll(newList);
		}

		String code = obj.getErrCode();
		obj.dispose();
		
		if (funs==null){
			return XmlCodeParser.formatResultTips(false, code);
		}
		return XmlCodeParser.formatResultData(true, code, CJsonTool.listBeanToJson(funs));
	}
	
	/**to query runtime log
	 * @param req HttpServletRequest of current user
	 * @param resp HttpServletResponse of current user
	 * @param p function parameter, format: p={"fromTime":yyyyMMddhhmmss,"toTime":yyyyMMddhhmmss,"page",n,"pageSize":m}
	 * @param sid session id of current user
	 * @author by fuzhou huilin corp.ltd lms 2016.08.03
	 */
	public String queryRuntimeLog(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		long fromTime = CJsonTool.getLongValue(p, "fromTime", 0);
		long toTime = CJsonTool.getLongValue(p, "toTime", 0);
		int page = CJsonTool.getIntValue(p, "page", 1);
		if (page<1) {
			page = 1;
		}
		int pageSize = CJsonTool.getIntValue(p, "pagesize", CConsts.PAGE_SIZE);
		if (pageSize<1) {
			pageSize = CConsts.PAGE_SIZE;
		}

		CUserUtils obj = new CUserUtils();
		List<RuntimeLog> logs = obj.queryRuntimeLog(page, pageSize, fromTime, toTime);
		String code = obj.getErrCode();
		if (logs==null){
			obj.dispose();
			return XmlCodeParser.formatResultTips(true, code);
		}

		int rows = obj.queryRuntimeLog(fromTime, toTime);
		int pages= Math.round((rows+pageSize-1)/pageSize);
		obj.dispose();
		String data = "[";
		String row;
		for(RuntimeLog lg: logs){
			row = CJsonTool.beanToJson(lg);
			row = CJsonTool.addNode(row, "logTime", DateTimeTool.dateToDateTimeString(lg.getLogTime()));
			data += row+",";
		}
		if(logs.size()>0) {
			data = StrTool.deleteCharAt(data, data.length() - 1) + "]";
		} else {
			data += "]";
		}
		
		return XmlCodeParser.formatResultPageData(code, page, pages, pageSize, rows, data);
	}
	
	//查询系统日志详情
	public String queryRLDetail(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		Long id = CJsonTool.getLongValue(p, "id", -1);
		if(id<0){
			return XmlCodeParser.formatResultTips(false,SysErr.SYS_PARAM_ERR);
		}
		
		CUserUtils cuUtils = new CUserUtils();
		RuntimeLog logDetail = cuUtils.queryRLDetail(id);
		String code = cuUtils.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			return XmlCodeParser.formatResultTips(false, code);
		}
		return XmlCodeParser.formatResultData(true, code, CJsonTool.beanToJson(logDetail));
	}

	public String qryCrmUserList(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		CUserUtils cuUtils = new CUserUtils();
		List<User> list =  cuUtils.qryCrmUserList();
		String code = cuUtils.getErrCode();
		return XmlCodeParser.formatResultData(true, code, CJsonTool.listBeanToJson(list));
	}
}